package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import Control.CStatistics;

/**
 * Servlet implementation class Statistics
 */
public class Statistics extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Statistics() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);

	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			CStatistics cs = new CStatistics();
			Map map = request.getParameterMap();
			int Type = 0;
			String type;
			if (map.containsKey("RankType")) {
				type = request.getParameter("RankType");
				if (type.equals("UserSeeRank")) {
					Type = 1;
				} else if (type.equals("UserCollectionRank")) {
					Type = 2;
				} else if (type.equals("StarRank")) {
					Type = 3;
				} else if (type.equals("SearchLogByTypeRank")
						&& map.containsKey("KeyWordType")
						&& map.containsKey("StartTime")
						&& map.containsKey("EndTime")) {
					Type = 4;
				} else if (type.equals("SearchLogByDateCount")
						&& map.containsKey("StartTime")
						&& map.containsKey("EndTime")) {
					Type = 5;
				}
				switch (Type) {
				case 1: {
					out.println(JSONArray.fromObject(cs
							.getCollectionRank("UserSee")));
				}
					;
					break;
				case 2: {
					out.println(JSONArray.fromObject(cs
							.getCollectionRank("Favourite")));
				}
					;
					break;
				case 3: {
					out.println(JSONArray.fromObject(cs.getStarRank()));
				}
					;
					break;
				case 4: {
					String startTime = request.getParameter("StartTime");
					String endTime = request.getParameter("EndTime");
					String KeyWordType = request.getParameter("KeyWordType");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
					Date StartTime = (Date) sdf.parse(startTime);
					Date EndTime = (Date) sdf.parse(endTime);
					out.println(cs.querySearch_Log(
							KeyWordType, StartTime, EndTime));
				}
					;
					break;
				case 5: {
					String startTime = request.getParameter("StartTime");
					String endTime = request.getParameter("EndTime");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
					Date StartTime = (Date) sdf.parse(startTime);
					Date EndTime = (Date) sdf.parse(endTime);
					out.println(cs.querySearch_Log(
							StartTime, EndTime));
				}
					;
					break;
				default: {
					out.println(new JSONObject().accumulate("Status", "ERROR!")
							.accumulate("RankType", type));

				}
					;
					break;
				}
			}

		}

		catch (ParseException e) {
			e.printStackTrace();
		} finally {

			out.close();
		}
	}

}
